/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Fingerprint.h"

REGISTER_PLUGIN_CLASS(Fingerprint)
bool Fingerprint::execute(const string&  action, cJSON* args, CallbackContext cbc) 
{
    if(action == "authenticate") {
        cJSON* pJson = cJSON_GetArrayItem(args, 0);
        if(pJson != nullptr) {
            char* pStr = cJSON_Print(pJson);
            m_cbc = cbc;
            executeArkTs("./Fingerprint/Fingerprint/Fingerprint", 0, pStr, "Fingerprint", cbc);
            free(pStr);
        }
    }
    
    if(action == "registerBiometricSecret") {
       cJSON* pJson = cJSON_GetArrayItem(args, 0);
        if(pJson != nullptr) {
            char* pStr = cJSON_Print(pJson);
            m_cbc = cbc;
            executeArkTs("./Fingerprint/Fingerprint/Fingerprint", 1, pStr, "Fingerprint", cbc);
            free(pStr);
        }
    }
    
    if(action == "loadBiometricSecret") {
       cJSON* pJson = cJSON_GetArrayItem(args, 0);
        if(pJson != nullptr) {
            char* pStr = cJSON_Print(pJson);
            m_cbc = cbc;
            executeArkTs("./Fingerprint/Fingerprint/Fingerprint", 2, pStr, "Fingerprint", cbc);
            free(pStr);
        }
    }
    
    if(action == "isAvailable") {
        m_cbc = cbc;
        return executeArkTs("./Fingerprint/Fingerprint/Fingerprint", 3, "", "Fingerprint", cbc);
    }
    
    if(action == "onArKTsResult") {
        return onArKTsResult(args);
    }
    return true;
}

bool Fingerprint::onArKTsResult(cJSON* args)
{
    string action = cJSON_GetObjectItem(args, "content")->valuestring;
    cJSON* json = cJSON_GetObjectItem(args, "result");
    if(action == "authenticate") {
        if(json != NULL && json->type == cJSON_Array) {
            int count = cJSON_GetArraySize(json);
            if(count > 0) {
                string result = cJSON_GetArrayItem(json, 0)->valuestring;
                if(result == "success") {
                    m_cbc.success();
                } else {
                    cJSON* pRet = cJSON_Parse(result.c_str());
                    if(pRet != nullptr) {
                        m_cbc.error(pRet);
                        cJSON_Delete(pRet);
                    } else {
                        m_cbc.error(result);
                    }
                }
            }
        }
    }
    
    if(action == "isAvailable") {
        if(json != NULL && json->type == cJSON_Array) {
            int count = cJSON_GetArraySize(json);
            if(count > 0) {
                string result = cJSON_GetArrayItem(json, 0)->valuestring;
                m_cbc.success(result);
            }
        }
    }
    return true;
}